package com.learning.spark.scala.core.wordcount

import org.apache.spark.{SparkConf, SparkContext}

/**
  * ClassName: WordCount
  * Description: 
  * Date: 2018/6/13 15:56 【需求编号】
  *
  * @author Sam Sho
  * @version V1.0.0
  */
object WordCount {
  def main(args: Array[String]): Unit = {

    val conf = new SparkConf().setMaster("local").setAppName("WordCount_Scala")
    val context = new SparkContext(conf)

    val text = context.textFile("wordCount.txt")
    val words = text.flatMap { line => line.split(" ") }
    val pairs = words.map { word => (word, 1) }
    val results = pairs.reduceByKey(_ + _)
      // 按值排序
      .map(tuple => (tuple._2, tuple._1))
    val sorted = results.sortByKey(false)
    sorted.foreach { x => println(x) }


    //    val pairs = words.map { word => (word, 1) }.reduceByKey(_ + _).map(tuple => (tuple._2, tuple._1)).sortByKey(false).foreach(x => println(x))


  }

}
